-- weaken_by_floor
-- created by panyl
-- 削弱目标（取消和地牢层数的关联）数值为 威力 * 魔力/（30+魔力）

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return {};
        end

        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[3] > 0 then
            -- 不包括本回合
            effectRound = round + para[3] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 魔力
        local magic = source:getMagic();
        -- 被对方百分比削弱
        prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 威力
        local force = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        local force = math.modf(force * (addon_rate + 1000) / 1000);

        -- 千分比
        local v = math.modf((1000 * magic + (magic + 30) * (force - 1000)) / (magic + 30));
        v = math.modf(para[2] * v / force);

        -- 如果敌方是boss，效果减半
        if target:isBoss() then
            v = math.modf(v / 2);
        end

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        local condition = {
            ["value"] = v,
            ["end_round"] = effectRound,
        };

        if "ruby_ray" == para[1] then
            prop = PropM.combine(source, "slime_strengthen2", para[1])
            condition["drop_rate"] = prop[3];
        end

        -- 附加状态
        CombatStatusM.applyStatus(target, para[1], condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)

        -- 魔力
        local magic = source:getMagic();

        -- 威力
        local base = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 百分比,可以超出100%
        -- 因为是减弱的，数值可能为负，这里显示用正数
        local multi = math.abs(para[2] / 10);
        --local vbase = math.modf(multi * math.modf((1000 * magic + (magic + 30) * (force - 1000)) / (magic + 30)) / base);
        local vforce = math.modf(multi * math.modf((1000 * magic + (magic + 30) * (force - 1000)) / (magic + 30)) / force);
        --local vaddon = vforce - vbase;
        desc = string.gsub(desc, "{weaken_by_floor}", vforce);

        -- 回合数的显示
        local baseRound = para[3];
        local round = para[3];
        -- 增加持续时间的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        if "ruby_ray" == para[1] then
            prop = PropM.combine(source, "slime_strengthen2", para[1])
            local rate = math.modf(prop[3] / 10);
            desc = string.gsub(desc, "{drop_rate}", rate);
        end

        return desc;
    end,
};
